package weixin.idea.huodong.controller;

import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import weixin.idea.huodong.entity.HAcodeEntity;
import weixin.idea.huodong.entity.HAcodeHMEntity;
import weixin.idea.huodong.service.HAcodeServiceI;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Title: Controller
 * @Description: 自动发号
 * @author zhangdaihao
 * @date 2016-09-23 14:24:14
 * @version V1.0
 *
 */
@Controller
@RequestMapping("/hacodeController")
public class HAcodeController extends BaseController {

	private String message;
	@Autowired
	private SystemService systemService;
	@Autowired
	private HAcodeServiceI hacodeService;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 自动发号 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView hdRecord(HttpServletRequest request) {
		return new ModelAndView("weixin/idea/huodong/acode/acodeList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(HAcodeEntity hacode, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(HAcodeEntity.class, dataGrid);
		cq.eq(ACCOUNTID, ResourceUtil.getWeiXinAccountId());
		cq.add();
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hacode, request.getParameterMap());
		this.hacodeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 发号列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addOrUpdate")
	public ModelAndView addOrUpdate(HAcodeEntity hacode, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(hacode.getId())) {
			hacode = hacodeService.getEntity(HAcodeEntity.class, hacode.getId());
			req.setAttribute("hacodePage", hacode);
		}
		return new ModelAndView("weixin/idea/huodong/acode/acode");
	}

	/**
	 * 添加发号
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(HAcodeEntity hacode, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(hacode.getId())) {
			HAcodeEntity t = hacodeService.get(HAcodeEntity.class, hacode.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(hacode, t);
				JSONArray array = JSONArray.parseArray(hacode.getContent());
				if (array != null) {
					int sIndex = 0;
					Iterator<Object> it = array.iterator();
					while (it.hasNext()) {
						JSONObject json = (JSONObject) it.next();
						if (json.getInteger("s") != null && json.getInteger("s") == Integer.valueOf(1)) {
							sIndex++;
						}
					}
					t.setTotal(array.size());
					t.setSurplus(sIndex == 0 ? array.size() : array.size() - sIndex);
				}
				hacodeService.saveOrUpdate(t);
				message = String.format("%s%s", hacode.getGamename(), "更新成功");
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = String.format("%s%s", hacode.getGamename(), "更新失败");
			}
		} else {
			hacode.setAddtime(new Date());
			if (StringUtil.isNotEmpty(hacode.getContent())) {
				JSONArray array = new JSONArray();
				String[] cons = hacode.getContent().split(",");
				if (cons.length > 0) {
					for (String $ : cons) {
						JSONObject json = new JSONObject();
						json.put("n", $);
						json.put("s", 0);
						array.add(json);
					}
				}
				hacode.setTotal(array.size());
				hacode.setSurplus(array.size());
				hacode.setContent(array.toJSONString());
			}
			hacodeService.save(hacode);
			message = String.format("%s%s", hacode.getGamename(), "添加成功");
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 删除发号
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(HAcodeEntity hacode, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		hacode = systemService.getEntity(HAcodeEntity.class, hacode.getId());
		message = String.format("%s%s", hacode.getGamename(), "删除成功");
		hacodeService.delete(hacode);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		j.setMsg(message);
		return j;
	}

	/**
	 * 已发号码列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goHacodeRecord")
	public ModelAndView goHacodeRecord(HttpServletRequest request) {
		String hdId = request.getParameter("haid");
		request.setAttribute("haid", hdId);
		return new ModelAndView("weixin/idea/huodong/acode/acodeHM");
	}

	/**
	 * 已发号码
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */
	@RequestMapping(params = "datagridHM")
	public void datagridHM(HAcodeHMEntity hacode, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		String hdid = request.getParameter("haid");
		CriteriaQuery cq = new CriteriaQuery(HAcodeHMEntity.class, dataGrid);
		cq.eq("haid", hdid);
		cq.eq(ACCOUNTID, ResourceUtil.getWeiXinAccountId());
		cq.add();
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hacode, request.getParameterMap());
		this.hacodeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

}
